package org.glut.wechat.chat.controller;

import org.glut.wechat.chat.domain.Group;
import org.glut.wechat.chat.service.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:5174"}, allowCredentials = "true")
@RequestMapping("group")
public class GroupController {

    @Autowired
    private IGroupService groupService;

    // 1. 创建群聊（参数：群主ID、群名称）
    @PostMapping("/create")
    public String createGroup(@RequestParam("ownerId") String ownerId, @RequestParam("groupName") String groupName) {
        return groupService.createGroup(ownerId, groupName); // 返回群ID
    }

    // 2. 邀请成员进群（参数：群ID、邀请者ID、被邀请者ID）
    @PostMapping("/invite")
    public boolean inviteMember(
            @RequestParam("groupId") String groupId,
            @RequestParam("inviterId") String inviterId,
            @RequestParam("beInvitedUserId") String beInvitedUserId
    ) {
        return groupService.inviteMember(groupId, inviterId, beInvitedUserId);
    }

    // 3. 获取群成员列表（参数：群ID）
    @GetMapping("/members/{groupId}")
    public Set<String> getGroupMembers(@PathVariable("groupId") String groupId) {
        return groupService.getGroupMembers(groupId);
    }

    // 4. 获取用户的所有群（参数：用户ID，用于前端加载群会话）
    @GetMapping("/user/{userId}")
    public List<Group> getUserGroups(@PathVariable("userId") String userId) {
        return groupService.getUserGroups(userId);
    }

    // 5. 获取群信息（参数：群ID，用于前端显示群名/群主）
    @GetMapping("/info/{groupId}")
    public Group getGroupInfo(@PathVariable("groupId") String groupId) {
        return groupService.getGroupById(groupId);
    }
}
